<?php
/*
 * @Author: Terry
 * @Date: 2024-02-08 09:05:27
 * @LastEditors: Terry
 * @LastEditTime: 2024-05-17 14:26:39
 * @Description: file content
 * @FilePath: \auxface_api\app\certificate\controller\user\v1\Train.php
 */

declare (strict_types = 1);

namespace app\certificate\controller\user\v1;


use think\facade\Db;
use think\Request;

class Train{
    /**
     * @Description: 获取签到记录
     * @Author: Terry
     * @Date: 2024-04-28 22:02:47
     */    
    public function getList(Request $request){
        $wxappUserUuid = $request->uuid;
        $data  = $request->data;
        $page =  (int)$data['page'];
        $limit =  (int)$data['limit'];
        $searchWhere = [
            ['tci.wxappUserUuid','=',$wxappUserUuid],
            ['t.projectCode','=',$request->prjCode],
        ];

        if(!empty($data['name'])) array_push($searchWhere,['t.name','like',"%".$data['name']."%"]);

        $list = DB::table('certificate_trainCheckIn')
        ->alias('tci')
        ->join('certificate_train t','t.uuid = tci.trainUuid')
        ->join('projectInfo pi','pi.code = t.projectCode')
        ->join('projectinfo_projectCorpInfo cbi','cbi.corpCode = t.corpCode')
        ->where($searchWhere)
        ->page($page,$limit)
        ->field(['tci.inDate','t.name','pi.name as projectName','cbi.corpName','t.uuid','t.lecturer'])
        ->distinct(true)
        ->select();

        return result(SUCCESS,$list,'');
    }
    /**
     * @Description: 获取数据
     * @Author: Terry
     * @Date: 2024-02-08 09:15:44
     * @param {Request} $request
     */    
    public function getData(Request $request){
        $data = $request->data;
        $wxappUserUuid = $request->uuid;
        $uuid = $data['uuid'];
        $data = DB::table('certificate_train')
        ->alias('t')
        ->join('projectInfo pi','pi.code = t.projectCode')
        ->join('projectinfo_projectCorpInfo cbi','cbi.corpCode = t.corpCode')
        ->leftJoin('certificate_trainCheckIn tci',"tci.trainUuid = t.uuid and tci.wxappUserUuid = '{$wxappUserUuid}'")
        ->where([
            't.uuid'=>$uuid,
            't.isDelete'=>0,
        ])
        ->field(['pi.name as prjName','corpName','workerName','expiredTime','t.remark','t.name','tci.inDate','t.lecturer'])
        ->find();
        if($data){
            $expired = false;
            if(strtotime($data['expiredTime']) < time())  $expired = true;
            $data['expired'] = $expired;
        }
        return result(SUCCESS,$data,'');
    }   


    /**
     * @Description: 申请数据
     * @Author: Terry
     * @Date: 2024-02-20 18:23:28
     * @param {Request} $request
     */    
    public function checkIn(Request $request){
        $data  = $request->data;
        $wxappUserUuid = $request->uuid;
        $uuid = $data['trainUuid'];
        
        $expiredTime =  DB::table('certificate_train')->where(['isDelete'=>0,'uuid'=>$uuid])->value('expiredTime');
        if(!$expiredTime)  return result(FAIL,'','培训已被删除或错误！');
        if(strtotime($expiredTime) < time())  return result(FAIL,'','该培训签到已截至，无法签到!');
        
        $checkInData  = DB::table('certificate_trainCheckIn')->where([
            'trainUuid'=>$uuid,
            'wxappUserUuid'=>$wxappUserUuid,
            'isDelete'=>0,
        ])
        ->field(['workerName','inDate'])->find();
        if($checkInData) return result(SUCCESS,$checkInData,'签到成功！');
        $workerName = DB::table('certification_workerInfo')->where('wxappUserUuid',$wxappUserUuid)->value('name');

        DB::startTrans();
        try {
            $insertData = [
                'workerName'=>$workerName,
                'trainUuid'=>$data['trainUuid'],
                'uuid'=>createUuid(),
                'wxappUserUuid'=>$wxappUserUuid,
            ];
            DB::table('certificate_trainCheckIn')->insert($insertData);
            DB::commit();
            $insertData['inDate'] = date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']);
            return result(SUCCESS,$insertData,'签到成功！');
        } catch (\Exception $e) {
            DB::rollback();
        }
        return result(FAIL,'','签到失败,请重试');
    }

}
